/**
 * Created by James on 2016/12/20.
 */
function extend(Child, Parent) {
    /**
     *��ļ̳�
     * constructor���Է��ش����˶�������ݺ���������
     * prototype  ����ʹ���������������������Ժͷ���
     * uber     ����ֱ��ָ�򸸶����prototype����(uber��һ�������,��˼��"����", "��һ��")�������
     *          �Ӷ����ϴ�һ��ͨ��,����ֱ�ӵ��ø�����ķ���.���������ڱ�������,��Ҫ��Ϊ��ʵ�ּ̳е��걸��.
     **/
    var F = function () {
    };
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.uber = Parent.prototype;
}


(function (window, undefined) {
    var JqueryTable = (function ($ele, config) {
        return new JqueryTable.fn.init($ele, config);
    });
    JqueryTable.fn = JqueryTable.prototype = {
        constructor: JqueryTable,
        class_name: 'JqueryTable',
        init: function($ele, config) {
            this.ele = $ele ? $ele : null;
            this.config = config ? config : null;
            this.title = this.config['title'] ? this.config['title'] : null;
            this.columns = this.config['columns'] ? this.config['columns'] : [];
            this.dataset = this.config['dataset'] ? this.config['dataset'] : [];
            this.limit = this.config['limit'] ? this.config['limit'] : 10000;

            if (get_type(config) != "Object"){      //config 必须是一个字典对象
                throw new TypeError("config must be an Object!");
            }
            if(this.title){                        //如果有title项,表头就显示为title项(title的长度需与columns一致
                if (get_type(this.title) != "Array"){
                    throw new TypeError("title must be an Array!");
                }
                var len1 = this.title.length;
                var len2 = this.columns.length;
                if(len1 != len2){
                    throw new TypeError("title and columns' length must be equals!");
                }
            }
            this.createTable(this.ele, this.dataset);
        },
        template : {
            tr: function(){return '<tr>{}</tr>';},
            td: function(){return '<td>{}</td>';},
            th: function(){return '<th>{}</th>';},
            thead: function(){return '<thead>{}</thead>';},
            tbody: function(){return '<tbody>{}</tbody>';},
            table: function(){return '<table class="JqueryTable">{}</table>';},
        },
        createTable : function ($ele, dataset) {
            //console.log('ele...........', $ele, dataset);
            var thead = this.createThead();
            var tbody = this.createTbody(dataset);
            var t = thead + tbody;
            var table = this.template.table().replace("{}", t);
            $ele.append(table);
        },
        createTr: function (row) {
            'use strict';
            var tr = this.template.tr();
            var td = this.template.td();
            var temp='';
            for(var i = 0; i<this.columns.length; i++){
                var k = this.columns[i];
                var t = row[k];
                temp += td.replace('{}', t);
            }
            tr = tr.replace("{}", temp);
            return tr;
        },
        createThead: function () {
            var thead = this.template.thead();
            var tr = this.template.tr();
            var th = this.template.td();
            var temp='';
            var title = this.columns;
            if(this.title){
                title = this.title;
            }
            for(var i = 0; i< title.length; i++){
                temp += th.replace('{}', title[i]);
            }
            tr = tr.replace("{}", temp);
            thead = thead.replace("{}", tr);
            return thead;
        },
        createTbody: function (dataset) {
            "use strict";
            var trs='';
            var count = 0;
            for(var i in dataset){
                if(count >= this.limit){
                    break;
                }
                var row = dataset[i];
                var tr = this.createTr(row);
                trs += tr;
                count += 1;
            }
            return trs;
        }

    };
    JqueryTable.fn.init.prototype = JqueryTable.fn;
    window.JqueryTable = JqueryTable;
    window.james = 'Hello friend, If you have any comment about this script, pls contact me \<james.xj.zhang@mail.foxconn.com\>';
})(window);